package controller.Common;

import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import service.Common.FileService;
import util.ImageUtil;
import util.ResponseMessage;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.HashMap;

@Controller
@RequestMapping("/File")
public class FileController {

    @Value("/Static-Resources/images/")
    private Path USER_PIC_DIR;

    @Value("http://47.96.104.134:8080")
    private String localRoute;

    @Value("125")
    private int DEFAULT_IMAGE_WIDTH;

    @Value("125")
    private int DEFAULT_IMAGE_HEIGHT;

    @Autowired
    private FileService fileService;

    @CrossOrigin(allowCredentials="true")
    @ResponseBody
    @PostMapping("/upload.do")
    public ResponseMessage upload(MultipartFile file,HttpServletRequest request){
//        HttpSession session=request.getSession();
//        User Current_logged_in_user = (User)session.getAttribute("user");
//        if (Current_logged_in_user == null){
//            return new ResponseMessage(ResponseMessage.ERROR,"用户未登录");
//        }
        return new ResponseMessage(ResponseMessage.ERROR,"上传文件测试",file.getSize());
//        if (file.isEmpty()){
//            return new ResponseMessage(ResponseMessage.ERROR,"上传文件为空");
//        }
//
//        //先得到文件的输入流
//        InputStream tempFileInputStream;
//        try {
//            tempFileInputStream = file.getInputStream();
//        } catch (IOException e) {
//            e.printStackTrace();
//            System.out.println("获取文件输入流失败");
//            return new ResponseMessage(ResponseMessage.ERROR,"文件无法读取");
//        }
//        //获取文件名
//        String md5FileName = fileService.getFileName(tempFileInputStream);
//        String filename = file.getOriginalFilename();
//
//        if (md5FileName == null){
//            //文件名解析错误
//            return new ResponseMessage(ResponseMessage.ERROR,"文件上传出错，请稍后再试");
//        }
//
//        HashMap<String,String> resultFileMap = new HashMap<>();
//
////        if (type.equals("user_header_pic")){
//            //请求上传文件：用户头像
//            //id值为用户id
//            if (!fileService.findFileIsExist(USER_PIC_DIR,md5FileName + ".jpg")){
//                //如果文件不存在，保存
//                try(InputStream in = file.getInputStream()) {
//                    //改变分辨率就保存了
//                    ImageUtil.changeImagesResolution(in,USER_PIC_DIR.resolve(md5FileName + ".jpg"),DEFAULT_IMAGE_WIDTH,DEFAULT_IMAGE_HEIGHT,false);
//                } catch (IOException e) {
//                    e.printStackTrace();
//                    return new ResponseMessage(ResponseMessage.ERROR,"上传失败01");
//                }
//            }
////            if (fileService.updateUserHeaderPic(Current_logged_in_user.getUserId(),USER_PIC_DIR.resolve(md5FileName + ".jpg").toString())){
//            if (fileService.updateUserHeaderPic("15171545538",USER_PIC_DIR.resolve(md5FileName + ".jpg").toString())){
//                resultFileMap.put("path",localRoute +  "/oneschool/images/" + md5FileName + ".jpg");
//                return new ResponseMessage(ResponseMessage.SUCCESS
//                        ,"上传成功"
//                        , resultFileMap.get("path"));
//            }else {
//                return new ResponseMessage(ResponseMessage.ERROR,"上传失败02");
//            }



//        }
//        else if(type.equals("meeting_room_pic")){
//            //请求上传文件：会议室图片
//            //id值为会议室id
//            if (!fileUploadService.findFileIsExist(ROOM_PIC_DIR,md5FileName + ".jpg")){
//                //如果文件不存在，保存
//                try(InputStream in = file.getInputStream()) {
//                    //改变分辨率就保存了
//                    ImageUtil.changeImagesResolution(in,ROOM_PIC_DIR.resolve(md5FileName + ".jpg"),DEFAULT_IMAGE_WIDTH,DEFAULT_IMAGE_HEIGHT,false);
//                } catch (IOException e) {
//                    e.printStackTrace();
//                    return new ResponseMessage(ResponseMessage.ERROR,"上传失败");
//                }
//            }
//            if (fileUploadService.updateRoomPic(id,ROOM_PIC_DIR.resolve(md5FileName + ".jpg").toString())){
//                resultFileMap.put("path",localRoute +  "/file/meeting_room_pic/" + md5FileName + ".jpg");
//                return new ResponseMessage(ResponseMessage.SUCCESS
//                        ,"上传成功"
//                        ,resultFileMap.get("path"));
//            }else {
//                return new ResponseMessage(ResponseMessage.ERROR,"上传失败");
//            }
//        }else if(type.equals("meeting_file")){
//            //请求上传文件：会议文件
//            //id值为会议id
//            String filetype = filename.substring(filename.lastIndexOf(".") + 1);
//            if (!fileUploadService.findFileIsExist(MEETING_FILE_DIR,md5FileName + filetype)){
//                //如果文件不存在，保存
//                try {
//                    file.transferTo(MEETING_FILE_DIR.resolve(md5FileName + filetype).toFile());
//                } catch (IOException e) {
//                    e.printStackTrace();
//                    return new ResponseMessage(ResponseMessage.ERROR,"上传失败");
//                }
//            }
//            MeetingData meetingData = new MeetingData();
//            meetingData.setData_meeting_id(id);
//            meetingData.setData_name(filename);
//            meetingData.setData_type((short)1);
//            meetingData.setData_address(MEETING_FILE_DIR.resolve(md5FileName + filetype ).toString());
//            if (fileUploadService.uploadMeetingFile(meetingData)){
//                resultFileMap.put("path",localRoute +  "/file/meeting_file/" + md5FileName + filetype);
//                return new ResponseMessage(ResponseMessage.SUCCESS
//                        ,"上传成功"
//                        ,resultFileMap.get("path"));
//            }else {
//                return new ResponseMessage(ResponseMessage.ERROR,"上传失败");
//            }
//        }else if(type.equals("meeting_recording")){
//            //请求上传文件：会议录音
//            //id值为会议id
//            String filetype = filename.substring(filename.lastIndexOf(".") + 1);
//            if (!fileUploadService.findFileIsExist(MEETING_RECORDING_DIR,md5FileName + filetype)){
//                //如果文件不存在，保存
//                try {
//                    file.transferTo(MEETING_RECORDING_DIR.resolve(md5FileName + filetype).toFile());
//                } catch (IOException e) {
//                    e.printStackTrace();
//                    return new ResponseMessage(ResponseMessage.ERROR,"上传失败");
//                }
//            }
//            //文件不存在，先保存
//            MeetingData meetingData = new MeetingData();
//            meetingData.setData_meeting_id(id);
//            meetingData.setData_name(filename);
//            meetingData.setData_type((short)2);
//            meetingData.setData_address(MEETING_RECORDING_DIR.resolve(md5FileName + filetype).toString());
//            if (fileUploadService.uploadMeetingFile(meetingData)){
//                resultFileMap.put("path",localRoute +  "/file/meeting_recording/" + md5FileName + filetype);
//                return new ResponseMessage(ResponseMessage.SUCCESS
//                        ,"上传成功"
//                        ,resultFileMap.get("path"));
//            }else {
//                return new ResponseMessage(ResponseMessage.ERROR,"上传失败");
//            }
//        }else {
//            //类型值错误，返回错误消息
//            return new ResponseMessage(ResponseMessage.ERROR,"类型值错误");
//        }
    }

}

